/*
 * SDLEngine.h
 *  Es el motor de la aplicación que se encarga de:
 *  	-	levantar eventos y transformarlos;
 *  	-	dibujar la pantalla
 */

#ifndef SDLENGINE_H_
#define SDLENGINE_H_

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "Event.h"
#include "Surface.h"
#include "Window.h"
#include <string>
#include "../common/Threads/Thread.h"
#include "../Server/ColaMensajesSDL.h"
#include "../Server/BattleMasterPlayerGame.h"
#include "widgets/PlayScenarioPanel.h"
#include "widgets/Tank.h"
#include "../common/Notifier/Notifier.h"
#include "../common/ConditionVariable/MutexEncapsulation.h"
#include "../gui/WindowHelper.h"



int drawThread(void *ptr);

class SDLEngine  {
public:
	SDLEngine(std::string screenTitle, int widthResolution, int heightResolution, int bitsPerPixel);
	virtual ~SDLEngine();

	virtual void stop();

	void setNewEvent(Event* event);
	Event* getLasEvent();

	void initPlayPanel(BattleMasterPlayerGame* game);

	Tank* addTank(int posX, int posY,std::string owner,int angle = 0,bool flaged = false);

	void removeTank(Tank* tank);

	void changeScreenTitle(std::string screenTitle);

	bool isRunning();

	void addExternalNotify(ColaMensajesSDL* cola);

	void* run();

	static SDL_Surface* loadSurface(std::string path);

	static TTF_Font* openFont(std::string path, int size);

	void setNewVideoMode(int w, int h);

	//levanta el evento de la cola de SDL y lo transforma en un Event
	Event* pollEvent();

	void updateActiveWindow();
private:
	void setVideoMode(int width, int height, int bpp, Uint32 flags);

	void refreshScreen();

	void setActiveWindow(Window* activeWindow);

	/* Inicializa el motor de la interfaz grafica. */
	virtual bool startEngine();


	void updateVideoMode();

	void setPlayPanelNotification(std::string notification);

	void filterUselessEvents();

	//Evito la copia
	SDLEngine(SDLEngine *){};

	Window* activeWindow;
	Surface* screen;
	std::string screenTitle;

	//Para el video mode
	int widthResolution;
	int heightResolution;
	int bitsPerPixel;

	//Estado
	bool running;

	//Mensajeria
	ColaMensajesSDL* salida;
	ColaMensajesSDL* entrada;

	Notifier* notifier;

	//Le mando todos los eventos que saca sdl
	ColaMensajesSDL* externalNotify;

	MutexEncapsulation mutex;

	WindowHelper* windowFactory;




	//Thread de dibujo
	SDL_Thread *thread;

};

#endif /* SDLEngine_H_ */
